// ==UserScript==
// @name         My Script
// @version      1.0.0
// @description  Awesome userscript
// @match        https://example.com/*
// ==/UserScript==
import { waitLastEventLoop } from "./utils";
import { waitCountEventLoop } from './newUtils/waitCountEventLoop';
import { } from 'tslib';
import { zhihu } from "./websites/zhihu.mjs";
import { bilibili } from "./websites/bilibili.mjs";
import { baidu_baijiahao } from "./websites/baidu_baijiahao.mjs";
import { zhihu_link } from "./websites/zhihu_link.mjs";
import { sogou_link } from "./websites/sogou_link.mjs";
import { baidu_zhidao } from "./websites/baidu_zhidao.mjs";
import { baidu_tieba } from "./websites/baidu_tieba.mjs";
import { douban } from "./websites/douban.mjs";
import { gitee } from "./websites/gitee.mjs";

type Func = (dom: HTMLElement) => any;

const funcOnlyDo = (selectorStr: string, func: Func) => {
  const dom = document.querySelector(selectorStr);
  const isDom = dom instanceof HTMLElement;
  if (isDom) {
    // dom.remove();
    // dom.style.display = 'none';
    func(dom);
  }
  return isDom;
};
export const funcAnythingLoop = (func: Func) => (selectorStr: string) => {
  return (function loop () {
    return waitLastEventLoop(() => {
      funcOnlyDo(selectorStr, func);
      waitLastEventLoop(() => loop());
    });
  })();
};
const removeDom = (dom: HTMLElement) => { dom.style.display = 'none'; };


export const funcRemove = (selectorStr: string) => {
  return waitLastEventLoop(() => {
    if (!funcOnlyDo(selectorStr, removeDom)) {
      waitLastEventLoop(() => funcRemove(selectorStr));
    }
  });
};
export const funcRemoveLoop = (selectorStr: string) => {
  // waitLastEventLoop(() => {
  //   funcOnlyDo(selectorStr, removeDom);
  //   waitLastEventLoop(() => funcRemoveLoop(selectorStr));
  // });
  return funcAnythingLoop(removeDom)(selectorStr);
};
export const funcClick = (selectorStr: string, count = 1) => {
  waitCountEventLoop(() => {
    const dom = document.querySelector(selectorStr);
    waitCountEventLoop(() => {
      if (dom instanceof HTMLElement) {
        dom.click();
      } else {
        waitCountEventLoop(() => {
          funcClick(selectorStr);
        }, count);
      }
    }, count);
  }, count);
};
export const funcRemoveAndClick = (
  { removeList = [], clickList = [] }: {
    readonly removeList?: ReadonlyArray<string>;
    readonly clickList?: ReadonlyArray<string>;
  }
) => {
  removeList.forEach(funcRemove);
  clickList.forEach(funcClick);
};

waitLastEventLoop(() => {
  const { href } = location;
  if (href.startsWith('https://www.zhihu.com/') || href.startsWith('https://zhuanlan.zhihu.com/')) {
    zhihu();
  }
  if (href.startsWith('https://m.bilibili.com/')) {
    bilibili();
  }
  if (href.startsWith('https://baijiahao.baidu.com/s?') || href.startsWith('https://mbd.baidu.com/') || href.startsWith('https://health.baidu.com/')) {
    baidu_baijiahao();
  }
  if (href.startsWith('https://link.zhihu.com/')) {
    zhihu_link();
  }
  if (href.startsWith('http://m.sogou.com/web/')) {
    sogou_link();
  }
  if (href.startsWith('https://zhidao.baidu.com/')) {
    baidu_zhidao();
  }
  if (href.startsWith('https://tieba.baidu.com/')) {
    baidu_tieba();
  }
  if (href.startsWith('https://m.douban.com/')) {
    douban();
  }
  if (href.startsWith('https://gitee.com/link?')) {
    gitee();
  }
});
// https://www.zhihu.com/question/447658844/answer/3595859664 https://zhuanlan.zhihu.com/p/574826528
//https://zhuanlan.zhihu.com/p/685397662
//https://m.bilibili.com/opus/540944168794727461
//https://baijiahao.baidu.com/s?id=1816387716026651045
//https://mbd.baidu.com/newspage/data/dtlandingshare?sourceFrom=search_s&nid=dt_5195871648274408431
//https://link.zhihu.com/?target=http%3A//www.quqi.com/
//http://m.sogou.com/web/id=67bffc93-27a1-46a4-8d03-08415e1b8f3e/keyword=%E7%99%BE%E5%BA%A6%E7%BD%91%E7%9B%98%20%E7%99%BE%E5%BA%A6%E7%BD%91%E7%9B%98%E9%9D%92%E6%98%A5%E7%89%88%20%E8%85%BE%E8%AE%AF%E5%BE%AE%E4%BA%91%20%E9%98%BF%E9%87%8C%E4%BA%91%E7%9B%98%20%E5%A4%B8%E5%85%8B%E7%BD%91%E7%9B%98%20UC%E7%BD%91%E7%9B%98%20%E9%92%89%E7%9B%98%20QQ%E9%82%AE%E7%AE%B1%E6%96%87%E4%BB%B6%E4%BA%91%E7%9B%98%20%E8%B1%86/sec=5R8ZdSKDxRkIC_0_I3VgWw../entryTime=1743515254775/vr=30000909/tc?rcer=gNz_aOqzA04K0lhik&dp=1&pid=sogou-waps-21a38ed2ee0c2c08&is_per=0&pno=1&pcurl=https%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F467640427&clk=1&wml=1&url=http%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F467640427&vrid=30000909&linkid=1&mcv=15&pcl=48,218&sed=0&ml=0&sct=0
//https://zhidao.baidu.com/question/444367212183066844.html
//https://tieba.baidu.com/p/8269028700#/
